package com.weisy.signin.ui.activity;

import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.weisy.signin.R;
import com.weisy.signin.adapter.FragmentAdapter;
import com.weisy.signin.base.BaseAcativity;
import com.weisy.signin.base.BaseFragment;
import com.weisy.signin.comm.Comm;
import com.weisy.signin.ui.fragment.MineFragment;
import com.weisy.signin.ui.fragment.MyScoreFragment;
import com.weisy.signin.ui.fragment.SignFragment;

import java.util.ArrayList;
import java.util.List;

import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;

/**
 * @author weisy
 * @function 学生首页
 * @date 2019/3/21 21:45
 */
@Route(path = Comm.ROUTE_STUDENT)
public class StudentActivity extends BaseAcativity {

    @BindView(R.id.navMenu)
    BottomNavigationView navMenu;
    @BindView(R.id.vpMain)
    ViewPager vpMain;

    private MenuItem menuItem;

    @Override
    public void findView() {
        setContentView(R.layout.activity_study);
    }

    @Override
    public void initView() {
        List<BaseFragment> fragments = new ArrayList<>();
        fragments.add(new MyScoreFragment());
        fragments.add(new SignFragment());
        fragments.add(new MineFragment());
        vpMain.setAdapter(new FragmentAdapter(getSupportFragmentManager(), fragments));
        vpMain.setCurrentItem(0);
        vpMain.setOffscreenPageLimit(2);
        navMenu.setOnNavigationItemSelectedListener(menuItem -> {
            switch (menuItem.getItemId()) {
                case R.id.item_score:
                    vpMain.setCurrentItem(0);
                    break;
                case R.id.item_sign:
                    vpMain.setCurrentItem(1);
                    break;
                case R.id.item_mine:
                    vpMain.setCurrentItem(2);
                    break;
            }
            return false;
        });

        vpMain.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (menuItem != null) {
                    menuItem.setChecked(false);
                } else {
                    navMenu.getMenu().getItem(0).setChecked(false);
                }
                menuItem = navMenu.getMenu().getItem(position).setChecked(true);
                menuItem.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public void initData() {

    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序",
                        Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                android.os.Process.killProcess(android.os.Process.myPid());   //获取PID
                System.exit(0);
                finish();
            }
        }
        return true;
    }
}
